<?php

//returns array of available languages - full name
function get_languages_list() {
	global $conf;
	
	$db = new dbquery;
	$db->cache('langnames', 'lang');
	$db->query("SELECT * FROM $conf[prefix]lang WHERE name = 'langname'");
	$d=$db->fetch_array(MYSQL_NUM);
	unset($d[0]);
	
	return $d;
}

//returns array of available languages - short name
function get_lang_list() {
	global $conf;
	
	$db = new dbquery;
	$db->cache('langs', 'lang');
	$db->query("SELECT * FROM $conf[prefix]lang WHERE name = 'lang'");
	$d=$db->fetch_array(MYSQL_NUM);
	unset($d[0]);
	
	return $d;
}

function get_lang() {
	global $conf;
	
	//include global language variables
	if($dh = opendir('lang/default/global/')) {
		while (($file = readdir($dh)) !== false) {
			if(!is_dir('lang/default/global/'.$file)) {
				if(file_exists('lang/'.$_SESSION['lang_short'].'/global/'.$file))
					include('lang/'.$_SESSION['lang_short'].'/global/'.$file);
				else
					@include('lang/default/global/'.$file);
			}
		}
		closedir($dh);
	}
	include('lang/'.$_SESSION['lang_short'].'/global.php');
	//
	 
	if ($_GET['module']) {
		if(file_exists('lang/'.$_SESSION['lang_short'].'/'.$_GET['module'].'.php'))
			include('lang/'.$_SESSION['lang_short'].'/'.$_GET['module'].'.php');
		else
			@include('lang/default/'.$_GET['module'].'.php');
	} else {
		if(file_exists('lang/'.$_SESSION['lang_short'].'/'.$conf['default_module'].'.php'))
			include('lang/'.$_SESSION['lang_short'].'/'.$conf['default_module'].'.php');
		else
			@include('lang/default/'.$conf['default_module'].'.php');
	}
}

//changing language
function change_language($redirect=true) {
	global $conf;

	if($_POST['language'])
	$lang=$_POST['language'];
	else
	$lang=$_GET['language'];

	$languages = get_lang_list();

	if(in_array($lang, $languages)) {
		$_SESSION['lang_short']=$lang;
		
		if($_SESSION['id']) {
			$db = new dbquery;
			$db->query("UPDATE $conf[prefix_users]users SET lang = '$_SESSION[lang_short]' WHERE login = '$_SESSION[login]'");
		}
	}

	if($redirect) redirect($_SESSION['redirect_1']);
}
//

//
function language() {
	global $conf, $lang;

	$langs = get_lang_list();

	$cont = '<form action="index.php" method="post"><table>
		<tr>
		     <td>
			 <select name="language" class="button">';

	foreach($langs as $l) {
		$cont .= "<option value=\"$l\"";
		if($l == $_SESSION['lang_short'])
		$cont .= ' selected';
		$cont .= ">$l</option>\n";
	}

	$cont .= '</select>
     </td>
</tr>
<tr>
     <td><input type="hidden" name="function" value="change_language" /><input type="submit" value="'.$GLOBALS['lang']['global']['change'].'" class="button" /></td>
</tr>
</table></form>';

	return $cont;
}
//

//funkcja wyswietlajaca w menu liste dostepnych jezykow
function language_flags($template='<a href="{url}" title="{alt}"><img src="{img}" alt="{alt}" alt="" /></a> ') {
	global $conf, $lang;

	$langs = get_lang_list();

	$cont='';

	foreach($langs as $l) {
		$cont.=string_template($template, array('url'=>'index.php?function=change_language&amp;language='.$l, 'alt'=>$l, 'lang'=>$l, 'img'=>'admin/gfx/flags/f0-'.$l.'.gif'));
	}

	return $cont;
}
//

//include lang files of $module
function lang_include($module){
	if(empty($module)) exit;
	
	$module=basename($module);
	if(file_exists('lang/'.$_SESSION['lang_short'].'/'.$module.'.php')) {
		include_once('lang/'.$_SESSION['lang_short'].'/'.$module.'.php');
	} else if(file_exists('lang/default/'.$module.'.php')) {
		include_once('lang/default/'.$module.'.php');
	}
}

//dodawanie nowych pol do jezyka
function lang_add($name='', $default, $pl='') {
	global $conf;
	
	if(empty($name)) exit;
	
	if(perms_check('admin', 'modules')) {
		
		$sql=array();
		$sqlI='';
		
		$languages = get_lang_list();
		foreach($languages as $l) {
			if($l=='pl')
				$sql[]="'".$pl."'";
			else
				$sql[]="'".$default."'";
			$sqlI=', `'.$l.'`';
		}
		
		$db = new dbquery;
		$db->query("INSERT INTO $conf[prefix]lang (`name` ".$sqlI.") VALUES ('".$name."', ".implode(', ', $sql).")");
	}

}

//dodawanie pol do tabeli przechowujcej informacje do instalacji nowych jezykow
function lang_fields_add($table, $fields, $types, $defaults) {
	global $conf;
	
	if(!$table) exit;
	
	if(perms_check('admin', 'modules')) {
		$db = new dbquery;
		$db->query("INSERT INTO $conf[prefix]lang_fields (`table`, `fields`, `types`, `defaults`) VALUES ('".$table."', '".$fields."', '".$types.", ".$defaults."')");
	}
}

//dodawanie pol do tabeli przechowujcej informacje do instalacji nowych jezykow
function lang_fields_delete($table) {
	global $conf;
	
	if(!empty($table)) exit;
	
	if(perms_check('admin', 'modules')) {
		$db = new dbquery;
		$db->query("DELETE FROM $conf[prefix]lang_fields WHERE table='".$table."'");
	}
}
?>
